<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="废物类别:">
          <el-input v-model="formInline.wastType" clearable />
        </el-form-item>
        <el-form-item label="废物代码:">
          <el-input v-model="formInline.codeContent" clearable />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">

      <!-- 加载动态菜单 -->
      <el-form :inline="true" class="form-inline">
        <el-form-item v-for="item in toolbtn" :key="item.id" >
          <el-button type="primary" :icon='item.meta.icon' @click="showViews(item.name)">
            {{ item.meta.title }}
          </el-button>
        </el-form-item>
        <el-form-item>
          <div> 当前页统计： 容器总数量：{{ summaryWastCount }} 个 | 废物总重量：
            {{ summaryWastWeight }} 吨
          </div>
        </el-form-item>
      </el-form>

      <!-- <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" icon="el-icon-view" :disabled="disabled" @click="view">
            查看废物列表
          </el-button>
        </el-form-item>
        <el-form-item>
          <div> 当前页统计： 容器总数量：{{ summaryWastCount }} 个 | 废物总重量：
            {{ summaryWastWeight }} 吨
          </div>
        </el-form-item>
      </el-form> -->

      <div class="result" style="margin-top: -10px;">
        <el-table ref="multipleTable" v-loading="loading" border :data="tableData" tooltip-effect="dark"
                  style="width: 100%" align="center" @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="code" label="废物代码" />
          <el-table-column prop="parentId" label="废物类别" />
          <el-table-column label="危险特性">
            <template slot-scope="scope">
              <el-select v-model="tableData[scope.$index].value" multiple placeholder="无数据" disabled
                         style="width: 100%;"
              >
                <el-option v-for="item in WasteCharacteristics" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="hwSumContainer" label="容器数量" />
          <el-table-column prop="hwSumWastWeight" label="废物总重量 (吨)" />

        </el-table>
        <div class="page">
          <el-pagination background :current-page.sync="currentPage" layout="total,prev, pager, next,sizes"
                         :total="total" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @current-change="handleCurrentChange"
                         @size-change="handleSizeChange"
          />

        </div>
      </div>
    </div>

  </div>
</template>

<script>
// Table组件相关逻辑，需要引用
import {
  pageMixin
} from "../../../mixins/index";
import {
  ContainerType,
  PhysicalProperties,
  WasteCharacteristics
} from "../../../utils/resource.js";

import {
  // getWastCodeList_New
  getWastCodeList
} from '../../../api/warehouse.js';
import {
  // parseTime,
  getFloat
} from '../../../utils/index.js';
export default {
  filters: {
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
    },
    parseContainerType(value) {
      if (value != null) {
        return ContainerType.find((p) => p.value === `${value}`).label;
      }
    },
    parsePhysicalProperties(value) {
      if (value != null) {
        return PhysicalProperties.find((p) => p.value === `${value}`).label;
      }
    },

  },

  mixins: [pageMixin],
  data() {
    return {
      formInline: {
        wastType: "",
        codeContent: "",
      },
      containerTotal: 0,
      weightTotal: 0,
      form: {},
      WasteCharacteristics: [],
      toolbtn: [] // 列表顶部按钮
    };
  },
  async created() {
    this.WasteCharacteristics = WasteCharacteristics;
    await this.init();
  },
  methods: {
    // 列表顶部功能按钮点击事件
    showViews(action){
      //console.log(action);
      switch(action)
      {
        case '查看废物列表':
          if(this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.view();
          break;
      }
    },
    async init() {
      this.tableData = [];
      this.loading = true;
      const {
        data,
        count
      } = await getWastCodeList({
        limit: this.pageSize,
        page: this.currentPage,
        searchParam: JSON.stringify({
          "Code": this.formInline.codeContent,
          "Type": this.formInline.wastType
        })
      });
      console.log(data);

      this.containerTotal = 0;
      this.weightTotal = 0;
      data.forEach(item => {
        item.value = item.value.split(',');

        this.containerTotal += item.hwSumContainer;
        this.weightTotal += item.hwSumWastWeight;

        item.id = item.code;
      })

      this.tableData = data;
      this.undataSummaryData(data);
      this.total = count;
      this.loading = false;

      // 初始化按钮数据
      this.toolbtn = JSON.parse(localStorage.getItem('库存查询'));
      //console.log(this.toolbtn);
    },
    undataSummaryData(tableDataList) {
      this.summaryWastCount = 0;
      this.summaryWastWeight = 0;

      tableDataList.forEach(item => {
        // var obj = WasteCharacteristics.find((p) => p.value === `${item.value}`);
        // if (obj) {
        //   item.value = obj.label;
        // }

        if (item.hwSumWastWeight < 0) {
          item.hwSumWastWeight = 0
        }
        this.summaryWastWeight += item.hwSumWastWeight;
        this.summaryWastCount += parseInt(item.hwSumContainer);
      })
      this.summaryWastWeight = getFloat(this.summaryWastWeight, 5)
    },
    view() {
      this.$router.push(`/warehouse/stock/index_wastObjList/${this.code}`);
    },

    async reset() {
      this.formInline = {
        codeContent: "",
        wastType: ""
      };
      await this.init();
    },
  },
};
</script>

<style lang="scss" scoped>
  .result {
    margin-top: 10px;
  }

  .page {
    margin-top: 20px;
    text-align: right;
    margin-bottom: 40px;
  }

  .show-result {
    margin-top: 10px;
  }
</style>
